home *** CD-ROM | disk | FTP | other *** search
- /*
- * Reset DTDBs
- * Resets desktop databases of mounted volumes.
- *
- * Source file: resetdtdbs.c
- * Written by Brian Gaeke. E-mail: <brg@dgate.org>
- * Created 28 December 1992.
- *
- * The source code to Reset DTDBs is copyrighted © 1992, 1993, 1995 by The
- * Dimensional Gate Company. All rights reserved worldwide except where
- * specifically permitted.
- *
- * Source Notes (These were formerly distributed in a separate file.)
- *
- * Reset DTDBs is very, very simple--shows a few dialogs, calls a few Desktop Manager
- * functions, and then restarts. If you are not familiar with the Desktop Manager, it
- * would do you good to read pages 9-44 to 9-60 in Inside Macintosh VI before you
- * read the source. The code simply cycles through the VCB and calls
- * PBDTCloseDown(), PBDTFlush(), and PBDTDelete() on each vRefNum in the VCB queue.
- * The done() macro compares its argument to the qTail of the VCB queue, and then
- * checks to see if there is only one mounted volume (as the case may be.) If these
- * are both true, then the whole VCB has been scanned, and the loop stops.
- *
- * The compiled program is now distributed with the source. I imagine (although I am
- * not sure) that it could be ported to MPW C very easily. You are specifically
- * permitted to use elements of the Reset DTDBs code in your programs, provided that
- * you include the phrase "Portions copyright © 1992, 1993, 1995 by The Dimensional
- * Gate Company" in your documentation and source code.
- *
- * Version 2.1 (Mar. 30, 1995): Added a quickie preferences option that allows
- * the user to set a "silent running" feature. When the opening dialog comes up the
- * user can hit 'Quit, but don't prompt next time' - this will change a word in the
- * resource fork of the application (a boo-boo stylistically, I know -- let it come
- * back to bite me! then I'll work on adding findfolder support -- this is supposed
- * to be simple) that will make reset dtdbs not prompt next time. Hold shift when
- * starting to set this flag back to normal. This is a clunky interface, to be sure,
- * but it will probably be replaced once I get around to making v3.0. (i.e. Never :)
- */
-
- #define done(vcb) ((vcb) == (vcbQHdr->qTail)) && ((vcbQHdr->qHead) != (vcbQHdr->qTail))
- #include <Processes.h>
- #include <AppleEvents.h>
- #include <ShutDown.h>
-
- enum
- {
- StartupAlrt = 128,
- ConfirmAlrt = 129,
- ErrorAlrt = 130,
- SuccessAlrt = 131,
- ToggleAlrt = 132,
- PrefRsrcID = 128
- };
-
- #define PrefRsrcType 'Pref'
- #define FinderSignature 'MACS'
- #define _TRUE_ 1
- #define _FALSE_ 0
-
- typedef short bool;
-
- /* 2.1 - Silent running states: 0 - not silent running, run like usual.
- * (PrefsRec.silent) 1 - yes silent running, don't prompt.
- */
- typedef struct PrefsRec
- {
- bool silent;
- } PrefsRec, **PrefsRsrc;
-
- PrefsRsrc prefs = NULL;
-
- PrefsRsrc GetPrefs(void);
- void TurnSilentOff_ifShiftDown(void);
- void TurnSilentOn(void);
- pascal void main(void);
- short OpenDatabase(short vRefNum);
- void DeleteDatabase(short dtRefNum);
- void PrepareDatabase(short dtRefNum);
- void InitMac(void);
- void DoDialogs(void);
- void KillFinder(void);
- void Panic(OSErr err, unsigned char *routine);
-
- PrefsRsrc GetPrefs(void)
- {
- PrefsRsrc temp;
-
- temp = GetResource(PrefRsrcType,PrefRsrcID);
- if (!temp)
- {
- if (ResError() == resNotFound)
- {
- temp = (PrefsRsrc) NewHandle(sizeof(PrefsRec));
- if (!temp)
- Panic(MemError(),"\pAllocating mem. for new pref. data");
- AddResource(temp,PrefRsrcType,PrefRsrcID,"\p");
- if (ResError() != noErr)
- Panic(ResError(),"\pWriting new pref. data");
- }
- else
- Panic(ResError(),"\pReading pref. data");
- }
- return temp;
- }
-
- bool ShiftKeyDown(void)
- {
- EventRecord event;
- Boolean gotEvent;
-
- gotEvent = EventAvail(everyEvent,&event);
- return ((event.modifiers & shiftKey) ? 1 : 0);
- }
-
- void TurnSilentOff_ifShiftDown(void)
- {
- /* We're sure we have a Pref resource now...I hope */
- if (ShiftKeyDown())
- {
- ParamText("\p not",
- "\pprompt you for confirmation before resetting, as usual.",
- "\pclick the 'Quit & run silently next time' button on the startup dialog.",
- "\p");
- (void)Alert(ToggleAlrt,NULL);
- (*prefs)->silent = _FALSE_;
- ChangedResource(prefs);
- if (ResError() != noErr)
- Panic(ResError(),"\pWriting pref. data");
- ExitToShell();
- }
- }
-
- void TurnSilentOn(void)
- {
- /* We're sure we have a Pref resource now...I hope */
- ParamText("\p",
- "\pNOT prompt you for confirmation before resetting the databases, so that "
- "it will run unattended.",
- "\phold the Shift key as you start up Reset DTDBs.",
- "\p");
- (void)Alert(ToggleAlrt,NULL);
- (*prefs)->silent = _TRUE_;
- ChangedResource(prefs);
- if (ResError() != noErr)
- Panic(ResError(),"\pWriting pref. data");
- ExitToShell();
- }
-
- pascal void main(void)
- {
- QHdrPtr vcbQHdr = GetVCBQHdr();
- VCB *aVCB = (VCB *) vcbQHdr->qHead;
- short itsVRefNum,
- itsDTRefNum;
-
- InitMac();
-
- prefs = GetPrefs();
- TurnSilentOff_ifShiftDown();
-
- if (!(*prefs)->silent)
- DoDialogs();
-
- KillFinder();
- while (!done(aVCB))
- {
- itsVRefNum = aVCB->vcbVRefNum;
- itsDTRefNum = OpenDatabase(itsVRefNum);
- PrepareDatabase(itsDTRefNum);
- DeleteDatabase(itsVRefNum);
- aVCB = (VCB *) aVCB->qLink;
- };
-
- if (!(*prefs)->silent)
- (void)Alert(SuccessAlrt,NULL);
- ShutDwnStart();
- }
-
- short OpenDatabase(short vRefNum)
- {
- DTPBRec dtpb;
- short dtRefNum;
- OSErr err;
-
- dtpb.ioCompletion = NULL;
- dtpb.ioNamePtr = NULL;
- dtpb.ioVRefNum = vRefNum;
- err = PBDTGetPath(&dtpb);
- if (err) Panic(err,"\pOpenDatabase");
- return dtpb.ioDTRefNum;
- }
-
- void DeleteDatabase(short vRefNum)
- {
- DTPBRec dtpb;
- OSErr err;
-
- dtpb.ioCompletion = NULL;
- dtpb.ioVRefNum = vRefNum;
- dtpb.ioIndex = 0;
- err = PBDTDelete(&dtpb,false);
- if (err) Panic(err,"\pDeleteDatabase");
- }
-
- void PrepareDatabase(short dtRefNum)
- {
- DTPBRec dtpb;
- OSErr err;
-
- dtpb.ioCompletion = NULL;
- dtpb.ioDTRefNum = dtRefNum;
- err = PBDTFlush(&dtpb,false);
- if (err) Panic(err,"\pPrepareDatabase--Flush");
-
- err = PBDTCloseDown(&dtpb);
- if (err) Panic(err,"\pPrepareDatabase--CloseDown");
- }
-
- void InitMac(void)
- {
- short x;
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent,0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- MaxApplZone();
- for(x=1;x<11;x++)
- MoreMasters();
- }
-
- void DoDialogs(void)
- {
- if (Alert(StartupAlrt,NULL) != ok) TurnSilentOn();
- if (Alert(ConfirmAlrt,NULL) != ok) ExitToShell();
- }
-
- void KillFinder(void)
- {
- ProcessSerialNumber psn,
- finder;
- ProcessInfoRec pir;
- OSErr err;
- AppleEvent theEvent;
- AEDesc theAddress;
- Boolean gotEvent;
- EventRecord event;
- short x;
-
- /* We have to do this because IM VI says not to call PBDTReset/PBDTDelete
- * "unless you are manipulating the desktop database in the absence
- * of the Finder"... and we all know that IM is the word of God... :)
- * So here goes...
- */
-
- psn.highLongOfPSN = 0;
- psn.lowLongOfPSN = kNoProcess;
-
- pir.processInfoLength = sizeof(ProcessInfoRec);
- pir.processAppSpec = NULL;
- pir.processName = NULL;
-
- while (GetNextProcess(&psn) != procNotFound)
- {
- (void)GetProcessInformation(&psn,&pir);
- if (pir.processSignature == FinderSignature)
- {
- finder = psn;
- break;
- }
- }
-
- /*** kill the app code swiped from C.K. Haun with minimal modifications ***/
- (void)AECreateDesc(typeProcessSerialNumber, (Ptr)&finder,
- sizeof(finder), &theAddress);
- (void)AECreateAppleEvent(kCoreEventClass, kAEQuitApplication,
- &theAddress, kAutoGenerateReturnID, kAnyTransactionID, &theEvent);
- AEDisposeDesc(&theAddress);
- AESend(&theEvent, NULL, kAENoReply + kAEAlwaysInteract +
- kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
- AEDisposeDesc(&theEvent);
- /*** end swiped code ***/
-
- /* give it time to work */
- for(x=1; x<=5; x++)
- gotEvent = EventAvail(everyEvent, &event);
-
- }
-
- void Panic(OSErr err, unsigned char *routine)
- {
- Str255 errstr;
-
- if (prefs)
- {
- if (!(*prefs)->silent)
- {
- NumToString((long) err,errstr);
- ParamText(errstr,routine,"\p","\p");
- (void)Alert(ErrorAlrt,NULL);
- }
- }
- else
- {
- SysBeep(1);
- }
- ShutDwnStart();
- }
-
-